1103. 分糖果 II
为保证权益,题目请参考 1103. 分糖果 II(From LeetCode).
解决方案1
CPP
C++
#include <iostream>
#include <algorithm>
#include <vector>
using namespace std;
class Solution
{
public:
vector<int> distributeCandies(int candies, int num_people)
{
long index = 0;
vector<int> res(num_people, 0);
while (candies >= index + 1)
{
res[index % num_people] += index + 1;
index++;
candies -= index;
}
res[index % num_people] += candies;
return res;
}
};
int main()
{
Solution so;
so.distributeCandies(7,4);
return 0;
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30